package com.cqust.icc.aspect;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author 张成
 * @version 1.0
 * @date 2022/3/31 15:37
 */
public class AddLog {
    public static void extracted(String method_name) {
        Connection conn = null;
        Statement stmt = null;
        try {
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接对象
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zcc?serverTimezone=Asia/Shanghai&useSSL=false", "root", "root");
            //创建Statement对象
            stmt = conn.createStatement();


            //3. 执行添加

            String sql = "insert into tb_log(ip_address,method_name) values('127.0.0.1' ,'"+method_name+"')";
            System.out.println(sql);
            //影响的行数， ，如果大于0 表明操作成功。 否则失败
            int result = stmt.executeUpdate(sql);

            if (result > 0) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {//避免空指针异常
                try {
                    stmt.close();//释放资源
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
